package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.Settings;
import com.tanhua.dubbo.mapper.QuestionMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class QuestionApiImpl implements QuestionApi {
    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 通过id查询陌生人问题
     * @param id
     * @return
     */
    @Override
    public Question findByUserId(Long id) {
        //构建查询条件
        QueryWrapper<Question> queryWrapper = new QueryWrapper<Question>();
        queryWrapper.eq("user_id",id);
        return questionMapper.selectOne(queryWrapper);
    }

    /**
     * 设置陌生人问题
     * @param question
     */
    @Override
    public void updateQuestion(Question question) {
        Question questionInDb = findByUserId(question.getUserId());
        if (null==questionInDb){
            questionMapper.insert(question);
        }else {
            question.setId(questionInDb.getId());
            questionMapper.updateById(question);
        }
    }
}
